/**
 * Created by lwy on 2017/7/24.
 */

var page = 0;
var num;
$(function(){
    doAjax();
})

function doAjax(){
    page++;
    $.ajax({
        url:'/cardC/getCardList.json',
        dataType:'json',
        data:{
            token:getItem('token'),
            pageNu:page,
        },
        type:'post',
        success:function(data){
            num = data.datas.cardList.length;
            for(var i=0;i<data.datas.cardList.length;i++){
                var isDefault = '';
                if(data.datas.cardList[i].isDefault){
                    isDefault = '../phoneImg/changecard.png';
                }else{
                    isDefault = '../img/nochangecard.jpg';
                }
            $('<div class="withdraw-box" id="'+data.datas.cardList[i].id+'">'+
                    '<div class="newWithdrawBankBox mui-clearfix">'+
                    '<div class="bankImg"><img id="bankImg" src="'+data.datas.cardList[i].bankImg+'" alt="银行图标"></div>'+
                    '<div class="bankInfo"><div class="bankInfo-name" id="bank">'+data.datas.cardList[i].bank+'</div>'+
                    '<div class="bankInfo-num" id="cardNumShort">尾号'+data.datas.cardList[i].cardNumShort+'</div></div>'+
                    '<span class="mui-icon mui-icon-arrowright"><img class="defaultImg" src="'+isDefault+'" alt="img"></span>'+
                    '</div></div>').appendTo($('#choosecardBox'));
            }
        }
    })
    return num;
}

//添加银行卡
$('#addcard').click(function(){
    window.location.href="addcard.html";
})

//上拉加载
mui.init({
    pullRefresh : {
        container:"#refreshContainer",//待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
        up : {
            contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
            contentnomore:'我是有底线的',//可选，请求完毕若没有更多数据时显示的提醒内容；
            callback :function(){
                setTimeout(function () {
                    var numResult = doAjax();;
                    if (numResult < 20) {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
                    } else {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(false);
                    }
                }, 1000);
            } //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
        }
    }
});

//点击更换默认银行卡
mui('body').on('tap','.withdraw-box',function(){
    var id = $(this).attr('id');
    $.ajax({
        url: '/cardC/toBeDefault.json',
        dataType: 'json',
        data: {
            token: getItem('token'),
            cardId: id,
        },
        type: 'post',
        success: function (data) {
            if(data.code==0){
                mui.toast(data.msg);
                return;
            }
            if(data.code==-1){
                window.location.href="login.html";
            }
            if(data.code==1){
                window.location.href="integral-withdraw1.html";
            }
        }
    })
})

